home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 12 / BBS in a box XII-2.iso / Files II / Prog / M / Mini.sit / mini / clock.c / clock.c
Encoding:
C/C++ Source or Header  |  1987-06-04  |  3.1 KB  |  126 lines  |  [TEXT/MPS ]

  1. /* This module keeps the time in the menu bar
  2.    We are going to use the time manager however the header file in
  3.    version 1 is incorrect. We will make the correct declarations here.
  4.    ( version two headers fixed the problem)
  5.    
  6.    Jerry LeVan
  7.    325 Boone Trail
  8.    Richmond Ky 40475
  9.   
  10. */
  11.  
  12. #include <windows.h>
  13. #include <events.h>
  14. #include <OSUtils.h>
  15.  
  16. /* MPW InLine Function/Procs  */
  17.  
  18. pascal void SetUpA51() extern 0x2f0d;    /* move.l a5,-(sp)  */
  19. pascal void SetUpA52() extern 0x2a78;    /* move.l $904,a5   */ 
  20. pascal void SetUpA53() extern 0x0904;    /* literal $904        */
  21.  
  22. #define SetUpA5()     \
  23.  SetUpA51();         \
  24.  SetUpA52();         \
  25.  SetUpA53()
  26.  
  27. pascal void RestoreA5() extern 0x2a5f;    /* move.l (sp)+,a5 */
  28.  
  29. /* MPW timer queue definitions */
  30.  
  31. typedef struct TMTask {
  32.     struct QElem *qLink;
  33.     short qType;
  34.     ProcPtr tmAddr;
  35.     short tmCount;
  36. } TMTask;
  37. pascal void InsTime(tmTaskPtr)
  38.     TMTask *tmTaskPtr;
  39.     extern ;
  40. pascal void PrimeTime(tmTaskPtr,count)
  41.     TMTask *tmTaskPtr;
  42.     long count;
  43.     extern ;
  44. pascal void RmvTime(tmTaskPtr)
  45.     TMTask *tmTaskPtr;
  46.     extern ;
  47.     
  48. Boolean showClock = true;    /* if true  show the time in menubar */    
  49.  static TMTask clockTsk = {0, drvQType, 0, 0 }; /* actually timer queue */
  50.  static GrafPort screen; /* we can draw on the entire screen using this port */
  51.  
  52. /*************************routines*********************************/
  53.  
  54. /* the routine should not do anything to "move" memory */ 
  55.  void clockTimer()  /* simply post an event */
  56.    { EventRecord event;
  57.      /* post event only if none are in the queue */
  58.     /* Debugger(); */
  59.      if(!OSEventAvail(app2Mask,&event)) PostEvent(app2Evt,0);
  60.    }
  61.  
  62.  pascal void TimerInterrupt()
  63.     {
  64.     SetUpA5(); clockTimer(); RestoreA5();
  65.     }
  66.     
  67.  void startClock()
  68.    {
  69.    clockTsk.tmAddr = TimerInterrupt;
  70.    InsTime(&clockTsk);
  71.    PrimeTime(&clockTsk,1000); /* and schedule the routine  1 second */
  72.    };
  73.    
  74.  void stopClock()
  75.    { GrafPtr oldPort;
  76.    
  77.    RmvTime(&clockTsk);
  78.    GetPort(&oldPort); /* save current port */
  79.    SetPort(&screen);
  80.    MoveTo(450,14);
  81.    DrawString("              ");
  82.    SetPort(oldPort);
  83.    
  84.    };
  85.    
  86.  void InitClock()
  87.     {
  88.       OpenPort(&screen);
  89.       TextMode(srcCopy);
  90.       if(showClock)startClock();
  91.      }
  92.  void showTime()
  93.    {
  94.         long secs;  /* current time */    
  95.         DateTimeRec date;         
  96.         char dateString[10];    
  97.         GrafPtr oldPort; /* temp storage */
  98.   
  99.       GetPort(&oldPort);
  100.       SetPort(&screen);
  101.       ReadDateTime(&secs);
  102.       Secs2Date(secs, &date);
  103.       if(date.hour>12) date.hour -= 12;
  104.       /* convert date record to c string */
  105.       dateString[0] = (date.hour / 10) + 48;
  106.       if(dateString[0] == 48)dateString[0]=' ';
  107.       dateString[1] = (date.hour % 10) + 48;
  108.       dateString[2] = ':';
  109.       dateString[3] = date.minute /10 +48;
  110.       dateString[4] = date.minute % 10 + 48;
  111.       dateString[5] = ':';
  112.       dateString[6] = date.second /10 +48;
  113.       dateString[7] = date.second % 10 +48;
  114.       dateString[8] = 0;
  115.       MoveTo(450,14);
  116.       
  117.       /* the interrupt routine can't do this because drawstring may
  118.          move memory
  119.       */
  120.       DrawString(dateString);
  121.       SetPort(oldPort);
  122.       /* If I rearm here it works */
  123.       PrimeTime(&clockTsk,1000); 
  124.    };
  125.    
  126.